<?php
/**
 * Copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author francissem
 * @copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */
require_once("Core/UrlTailParams.php");
require_once(dirname(__FILE__)."/Abstract.php");

class Core_Controller_Request_Content extends Core_Controller_Request_Abstract{

	private $module_content_path;
	private $tail;
	private $content_url;
	private $content_file;

	function __construct($headers,$all_site_tags){
		parent::__construct($headers,$all_site_tags);
			
		$module = Core_Module::getModule($this->module_name);
		$this->module_content_path = $module->getContentsLocation();
		$this->module_url = $module->getInfo("url");
		$this->tail = preg_replace("/^".str_replace('/','\/',$this->module_url)."/","",$this->cropped_url	);
		$tail_nodes = array_filter(explode('/',$this->tail));
		$ext_matchs=array();
		preg_match("/.*(\..*)(\?.*)?$/",$this->cropped_url,$ext_matchs);
			
		$ext=isset($ext_matchs[1])?"":".xml.php";
			
			
		if(count($tail_nodes)==0){
			$f_index=pathjoin($this->module_content_path,"index{$ext}");
			if(is_file($f_index)){
				$this->content_file =$f_index;
				$this->content_url = "index";
			}
		}else{
			for($i=count($tail_nodes);$i>=0;$i--){
				$u=implode("/",array_slice($tail_nodes,0,$i));
				$f=$this->module_content_path."/{$u}{$ext}";
					
				if(is_file($f)){
					$this->content_file =$f;
					$this->content_url = $u;
					break;
				}
				$f_index=pathjoin($this->module_content_path,$u,"index{$ext}");
					
				if(is_file($f_index)){
					$this->content_file =$f_index;
					$this->content_url = pathjoin($u,"index");
					break;
				}
			}
		}
			
	}

	function getTailParams(){
		static $ptail;
		if(!isset($ptail)){
			$ptail = new Core_UrlTailParams($this->tail);
		}
		return $ptail;
	}

	function getContentFile(){
		return $this->content_file;
	}

	function getContentUrl(){
		static $cu;
		if(!isset($cu)) {
			$cu = pathjoin($this->getModuleUrl(),$this->content_url);
		}
		return $cu;
	}

}
?>